package com.meida.module.bbs.provider.service.impl;

import com.meida.common.constants.CommonConstants;
import com.meida.common.base.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaDelete;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.ApiAssert;
import com.meida.module.bbs.client.entity.BbsComment;
import com.meida.module.bbs.client.entity.BbsContent;
import com.meida.module.bbs.provider.mapper.BbsCommentMapper;
import com.meida.module.bbs.provider.service.BbsCommentService;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.module.bbs.provider.service.BbsContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 帖子回复接口实现类
 *
 * @author flyme
 * @date 2019-12-06
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BbsCommentServiceImpl extends BaseServiceImpl<BbsCommentMapper, BbsComment> implements BbsCommentService {

    @Autowired
    private BbsContentService contentService;

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<EntityMap> listByContentId(Long contentId, Integer num) {
        Long userId = OpenHelper.getUserId();
        ApiAssert.isNotEmpty("contentId不能为空", contentId);
        CriteriaQuery cq = new CriteriaQuery(BbsComment.class);
        cq.select(BbsComment.class, "commentId", "replyContent", "replyImages","createTime");
        cq.select("user.userId", "user.nickName", "user.userName", "user.avatar");
        cq.addSelect("collecon_count(2,'BbsComment',commentId) dzNum");
        cq.addSelect("collecon_tag(2,'BbsComment',commentId," + userId + ") dzTag");
        cq.eq(BbsComment.class, "contentId", contentId);
        cq.limit(num);
        cq.createJoin("com.meida.module.user.client.entity.AppUser").setMainField("replyUserId");
        cq.orderByDesc("comment.createTime");
        return selectEntityMap(cq);
    }

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BbsComment comment, EntityMap extra) {
        BbsContent content = contentService.getById(comment.getContentId());
        ApiAssert.isNotEmpty("帖子已删除", content);
        Long userId = OpenHelper.getUserId();
        comment.setReplyState(CommonConstants.ENABLED);
        comment.setReplyUserId(userId);
        comment.setReplyToUserId(content.getUserId());
        return ResultBody.ok();
    }

    @Override
    public ResultBody afterAdd(CriteriaSave cs, BbsComment comment, EntityMap extra) {
        contentService.updateCommentNum(comment.getContentId(), 1);
        return ResultBody.msg("评论成功");
    }

    @Override
    public ResultBody afterDelete(CriteriaDelete cd, Long[] ids) {
        contentService.updateCommentNum(cd.getIdValue(), -1);
        return ResultBody.ok();
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BbsComment> cq, BbsComment comment, EntityMap requestMap) {
        Long userId = OpenHelper.getUserId();
        Long contentId = comment.getContentId();
        ApiAssert.isNotEmpty("contentId不能为空", contentId);
        cq.select(BbsComment.class, "commentId", "replyContent", "replyImages","createTime");
        cq.select("user.userId", "user.nickName", "user.userName", "user.avatar");
        cq.addSelect("collecon_count(2,'BbsComment',commentId) dzNum");
        cq.addSelect("collecon_tag(2,'BbsComment',commentId," + userId + ") dzTag");
        cq.eq(BbsComment.class, "contentId", comment.getContentId());
        cq.createJoin("com.meida.module.user.client.entity.AppUser").setMainField("replyUserId");
        cq.orderByDesc("comment.createTime");
        return ResultBody.ok();
    }

}
